package com.zaoniao.enstudy.infracore.web;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * @author llb666
 */
public class DataResponse<T> extends BaseResponse {

    /**
     * 响应中的数据
     */
    private final T data;

    public T getData() {
        return data;
    }

    private DataResponse(ErrorCode errCode) {
        super(errCode.getCode(), errCode.getMsg());
        this.data = null;
    }

    private DataResponse(ErrorCode errCode, String message) {
        this.status = errCode.getCode();
        this.msg = message;
        this.data = null;
    }

    @JsonCreator
    public DataResponse(@JsonProperty("status") Integer status,
                        @JsonProperty("msg") String msg,
                        @JsonProperty("data") T data) {
        super(status, msg);
        this.data = data;
    }

    private DataResponse(ErrorCode errCode, T data) {
        this.status = errCode.getCode();
        this.msg = errCode.getMsg();
        this.data = data;
    }

    public static <T> DataResponse<T> ok(T data) {
        return new DataResponse<>(SUCCESS_CODE,SUCCESS_MSG,data);
    }

    public static <T> DataResponse<T> fail(ErrorCode errorCode) {
        return new DataResponse<>(errorCode);
    }

    public static <T> DataResponse<T> fail(ErrorCode errorCode, String message) {
        return new DataResponse<>(errorCode, message);
    }

    public static <T> DataResponse<T> errorMsg(String msg) {
        return new DataResponse<>(500, msg, null);
    }

    public static <T> DataResponse<T> errorMap(T data) {
        return new DataResponse<T>(501, "error", data);
    }

}
